const encryptKey = 'azEaijI7BD6pPigFHidEXn95'
const encryptIV = 'D076D35C'
var pubkeyHex =
	'04AF0FCC45059AA342221352E5268614F2FF7A430497B156C0DEE6E751AB44E4957E9E69299E2CD38E25985B7BD34E0E7BBA683DE4725A6A8CD07E19BFF8BEF44D'

function GetQueryString() {
	let origin = window.location.origin;
	if (origin.includes('127') || window.location.href.includes('page-')) {
		var url = location.search; //获取url中"?"符后的字串
		var theRequest = new Object();
		if (url.indexOf("?") != -1) {
			var str = url.substr(1);
			strs = str.split("&");
			for (var i = 0; i < strs.length; i++) {
				theRequest[strs[i].split("=")[0]] = decode(strs[i].split("=")[1]);
			}
		}
		return theRequest;
	} else {
		let obj = {
			id: '',
			preview:''
		}
		if(window.location.href.includes('preview')){
			let path = window.location.href.split('?')[0].split('/');
			obj.preview = true;
			obj.id = path[path.length - 1];
		}else{
			let path = window.location.href.split('/');
			obj.id = path[path.length - 1];
		}
		
		
		return obj;
	}
}

function GetQueryString2() {
	let origin = window.location.origin;
	if (origin.includes('127') || window.location.href.includes('page-')) {
		var url = location.search; //获取url中"?"符后的字串
		var theRequest = new Object();
		if (url.indexOf("?") != -1) {
			var str = url.substr(1);
			strs = str.split("&");
			for (var i = 0; i < strs.length; i++) {
				theRequest[strs[i].split("=")[0]] = decode(strs[i].split("=")[1]);
			}
		}
		return theRequest;
	} else {
		var url = location.search; //获取url中"?"符后的字串
		var theRequest = new Object();
		if (url.indexOf("?") != -1) {
			var str = url.substr(1);
			strs = str.split("&");
			for (var i = 0; i < strs.length; i++) {
				theRequest[strs[i].split("=")[0]] = decode(strs[i].split("=")[1]);
			}
		}
		return theRequest;
	}
}

function decode(val){
	let str = ''
	try{
		str = window.decodeURIComponent(val)
	}catch(e){
		str = val
	}
	return str
}
function encode(val){
	let str = ''
	try{
		str = window.encodeURIComponent(val)
	}catch(e){
		str = val
	}
	return str
}
function SetNumber(val) {
	let value = val;
		if (value >= 1000 && value < 10000) {
			value = value / 1000 + 'K';
		} else if (value >= 10000) {
			value = value / 10000 + 'W';
		}
		return value
}

function SetNumber2(val, index) {
	let value = val;
	if (value >= 1000 && value < 10000) {
		value = (value / 1000).toFixed(index) + 'K';
	} else if (value >= 10000) {
		value = (value / 10000).toFixed(index) + 'W';
	}
	return value
}

function getRootPath() {
	let origin = window.location.origin;
	if (origin.includes('127')) {
		let rootPath = window.location.pathname.replace('/', '').split('/')[0];
		return origin + '/' + rootPath + '/src/';
	} else {
		return ''
	}
}

function imgPath(url) {
	let imgUrl = url ? url : '';
	let origin = window.location.origin;
	if (imgUrl.includes('http://') || imgUrl.includes('https://')) {
		return imgUrl;
	} else if (origin.includes('127')) {
		return 'http://demo.jeecms.com' + imgUrl
	} else {
		return origin + imgUrl;
	}
}

function downloadFile(res, name) {
	const content = res
	const blob = new Blob([content]);
	const fileName = name
	if ('download' in document.createElement('a')) { // 非IE下载
		const elink = document.createElement('a');
		elink.download = fileName;
		elink.style.display = 'none';
		elink.href = URL.createObjectURL(blob);
		document.body.appendChild(elink)
		elink.click()
		URL.revokeObjectURL(elink.href) // 释放URL 对象
		document.body.removeChild(elink)
	}
}

function SEO(query) {
	homeApi.getChannelDetail({
		id: query.id
	}, res => {
		let seo = JSON.parse(JSON.stringify(res.data.seo)) || {};
		let meta = document.createElement('meta');
		meta.name = seo.seoTitle || '' + ',' + seo.seoKeyword || '';
		meta.content = seo.seoDesc || ''
		document.getElementsByTagName('head')[0].appendChild(meta);
	})
}

function wuzhangai() { //无障碍
/*accessible-read start*/
	var script = document.createElement('script');
	script.src = rootPath + '/js/reading/abt.js';
	document.body.appendChild(script);
	let data = {
		abtJsUrl: rootPath + '/js/reading/abt.js',
		encryptionStr: "4cU3e84wRBad3qyPeq4wU7if3aTD-m3bAkdd33zAtc39AQ1e3oEQBb3qQnxv4vTjd74iBTWi4nUDiq4lUz-g3qBjei4jUTWi32ETih3ayk1v3eyk5c3czkte4cEPRv4fTnWf4c*8mw3qTlyd4Azjd73iZBeC4h*7dg4pADla4wz7G-4h*71g33ADla4pTnG74hZ6xg3oETpa4aT89v4vUDyb4vTnW*4aEQJv3bTn2q33A41b3dz41c3cy4lb3YMoNv",
		gbkJsUrl: rootPath + '/js/reading/gbk.js',
		helpHtmlUrl: rootPath + '/js/reading/help.html',
		pinyinJsUrl: rootPath + '/js/reading/pinyin.js',
		reading: true,
		skinCssUrl: rootPath + '/css/skin.css',
		voice: true,
	}
	script.onload = function() {
		ABTshow(data);
	}
	/*accessible-read end*/
}


// 3DES加密desEncrypt
function encrypt3DES(str, key = encryptKey, iv = encryptIV) {
	let cryptoKey = CryptoJS.enc.Utf8.parse(key)
	var cryptoIv = CryptoJS.enc.Utf8.parse(iv.substring(0, 8))
	let encrypted = CryptoJS.TripleDES.encrypt(str, cryptoKey, {
		iv: cryptoIv,
		mode: CryptoJS.mode.CBC,
		padding: CryptoJS.pad.Pkcs7
	})
	// 这里的encrypted不是字符串，而是一个CipherParams对象
	return encrypted.ciphertext.toString(CryptoJS.enc.Base64)
}

// 3DES解密desDecrypt
function decrypt3DES(str, key = encryptKey, iv = encryptIV) {
	let cryptoKey = CryptoJS.enc.Utf8.parse(key)
	let cryptoIv = CryptoJS.enc.Utf8.parse(iv)
	// 返回的是一个Word Array Object，其实就是Java里的字节数组
	let decrypted = CryptoJS.TripleDES.decrypt(str, cryptoKey, {
		iv: cryptoIv,
		mode: CryptoJS.mode.CBC,
		padding: CryptoJS.pad.Pkcs7
	})
	return decrypted.toString(CryptoJS.enc.Utf8)

}
// 随机数
function randomWord(randomFlag, min, max) {
	// randomFlag: Boolean 是否随机个数
	// min 最少个数
	// max 最大个数
	var str = ''
	var range = min
	var arr = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k',
		'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F',
		'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'
	]
	// 随机产生
	if (randomFlag) {
		range = Math.round(Math.random() * (max - min)) + min
	}
	var pos = 0
	for (var i = 0; i < range; i++) {
		pos = Math.round(Math.random() * (arr.length - 1))
		str += arr[pos]
	}
	return str
}
// 去登
function gotoLogin(type) {
	if (window.location.origin.includes('127')) {
		window.location.href = window.location.origin + '/frontjeecmsx2/src/page/login.html?' + 'loginStatus=' + type;
	} else {
		window.location.href = window.location.origin + '/page-login.html?' + 'loginStatus=' + type;
	}
}

// 评论时间转化
function getDateDiff(timeStr) {
	
	let dateStr = new Date(timeStr).getTime();
  var publishTime = dateStr / 1000;
  var  d_seconds;
  var  d_minutes;
  var  d_hours;
  var  d_days;
  var  timeNow = parseInt(new Date().getTime() / 1000);
  var  d;
    date = new Date(publishTime * 1000),
    Y = date.getFullYear(),
    M = date.getMonth() + 1,
    D = date.getDate(),
    H = date.getHours(),
    m = date.getMinutes(),
    s = date.getSeconds();
  //小于10的在前面补0
  if (M < 10) {
    M = '0' + M;
  }
  if (D < 10) {
    D = '0' + D;
  }
  if (H < 10) {
    H = '0' + H;
  }
  if (m < 10) {
    m = '0' + m;
  }
  if (s < 10) {
    s = '0' + s;
  }
 
  d = timeNow - publishTime;
  d_days = parseInt(d / 86400);
  d_hours = parseInt(d / 3600);
  d_minutes = parseInt(d / 60);
  d_seconds = parseInt(d);
  if (d_days > 0 && d_days < 3) {
    return d_days + '天前';
  } else if (d_days <= 0 && d_hours > 0) {
    return d_hours + '小时前';
  } else if (d_hours <= 0 && d_minutes > 0) {
    return d_minutes + '分钟前';
  } else if (d_seconds < 60) {
    if (d_seconds <= 0) {
      return '刚刚';
    } else {
      return d_seconds + '秒前';
    }
  } else if (d_days >= 3 && d_days < 30) {
    return M + '-' + D + ' ' + H + ':' + m;
  } else {
		return  timeStr
	}
}
